package com.zgr.ssm.service.impl;

import com.zgr.ssm.entity.User;
import com.zgr.ssm.mapper.UserMapper;
import com.zgr.ssm.service.IUserService;
import com.zgr.ssm.service.ex.PasswordFountException;
import com.zgr.ssm.service.ex.UsernameFountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User addUser(User user) {
        insert(user);
        return user;
    }

    @Override
    public User login(User user) {
        User data = findByUsername(user.getUsername());
        if(data != null){
            if(user.getPwd().equals(data.getPwd())){
                return data;
            }
            else{
                throw new PasswordFountException("密码错误");
            }
        }else{
            throw new UsernameFountException("用户名不存在");
        }
    }

    private Integer insert(User user){
        Integer rows = userMapper.insert(user);
        return rows;
    }

    private User findByUsername(String username){
        User user = userMapper.findByUsername(username);
        return user;
    }

}
